package com.loukou.lkt.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.loukou.lkt.company.service.UserService;
import com.loukou.lkt.lkt.entity.Privilege;
import com.loukou.lkt.lkt.entity.Role;
import com.loukou.lkt.lkt.req.SearchUserReq;
import com.loukou.lkt.lkt.resp.DataGrid;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.resp.UserResp;
import com.loukou.lkt.web.annotation.AuthCheck;

@Controller
@AuthCheck(privileges = {"user.manage"})
@RequestMapping("/user")
public class UserController extends BaseController{
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "viewUserList")
	public ModelAndView viewUserList(){
		ModelAndView mv = new ModelAndView("/user/userList");
		List<Role> roleList = userService.getAllRole();
		mv.addObject("roleList", roleList);
		List<Privilege> privilegeList = userService.getAllPrivileges();
		mv.addObject("privilegeList", privilegeList);
		return mv;
	}
	
	@RequestMapping(value = "/getUserData")
	@ResponseBody
	public DataGrid getUserData(SearchUserReq userReq){
		DataGrid dataGrid =  userService.queryUserPage(userReq);
		return dataGrid;
	}
	
	@RequestMapping(value = "/deleteUser")
	public ModelAndView deleteUser(@RequestParam(value = "id", required = false, defaultValue = "") int id){
		if(id >0){
			userService.deleteUser(id);
		}
		ModelAndView mv = new ModelAndView("/user/userList");
		List<Role> roleList = userService.getAllRole();
		mv.addObject("roleList", roleList);
		List<Privilege> privilegeList = userService.getAllPrivileges();
		mv.addObject("privilegeList", privilegeList);
		return mv;
	}
	
	@RequestMapping(value = "/editUser")
	public ModelAndView editUser(@RequestParam(value = "id", required = false, defaultValue = "") int id){
		ModelAndView mv = new ModelAndView("/user/editUser");
		if(id > 0){
			UserResp userResp = userService.getUserResp(id);
			mv.addObject("userInfo", userResp);
		}
		List<Role> roleList = userService.getAllRole();
		mv.addObject("roleList", roleList);
		return mv;
	}
	
	@RequestMapping(value = "deleteUserRole")
	@ResponseBody
	public RespDto<String> deleteUserRole(@RequestParam(value = "userId", required = true, defaultValue = "") int userId,
			@RequestParam(value = "roleId", required = true, defaultValue = "") int roleId){
		RespDto<String> resp = new RespDto<String>();
		userService.deleteUserRole(userId, roleId);
		return resp;
	}
	
	@RequestMapping(value = "addUserRole")
	@ResponseBody
	public RespDto<Role> addUserRole(@RequestParam(value = "userId", required = true, defaultValue = "") int userId,
			@RequestParam(value = "roleId", required = true, defaultValue = "") int roleId){
		RespDto<Role> resp = new RespDto<Role>();
		resp = userService.addUserRole(userId, roleId);
		return resp;
		
	}
	
	@RequestMapping(value = "saveUser" , method = RequestMethod.POST)
	@ResponseBody
	public RespDto<String> saveUser(@RequestParam(value = "userId", required = true, defaultValue = "") int userId,
			@RequestParam(value = "roleIds", required = true, defaultValue = "") int[] roleIds,
			@RequestParam(value = "realname", required = true, defaultValue = "") String realname,
			@RequestParam(value = "username", required = true, defaultValue = "") String username){
		RespDto<String> resp = new RespDto<String>();
		if(userId == 0){
			resp = userService.addUser(realname, username, roleIds);
		}else{
			resp = userService.editUser(userId, realname, username, roleIds);
		}
		return resp;
	}

}
